#include "linkfabric/MemLink.hpp"
#include "linkfabric/LinkFabric.hpp"
#include "linkfabric/Stream.hpp"
#include "linkfabric/Registry.hpp"
#include "linkfabric/Crypto.hpp"
#include "linkfabric/Compression.hpp"
#include <iostream>
#include <unistd.h>

using namespace linkfabric;

int main() {
    MemLink mem;
    if (!mem.init("/dev/memlink0")) {
        std::cerr << "Failed to init MemLink\n";
        return 1;
    }

    LinkFabric fabric(mem);
    Stream stream(fabric);
    Registry registry;
    registry.registerHost({"hostA","192.168.1.10",9000,{}});

    int ch = stream.open();
    stream.onStream(ch, [](uint8_t tag, const std::vector<uint8_t>& data) {
        auto dec = Crypto::decrypt(data, "secret");
        auto decomp = Compression::decompress(dec);
        std::cout << "Received: " << std::string(decomp.begin(), decomp.end()) << "\n";
    });

    std::string msg = "Hello, distributed world!";
    auto comp = Compression::compress({msg.begin(), msg.end()});
    auto enc  = Crypto::encrypt(comp, "secret");
    stream.sendStream(ch, 0x01, enc, 128);

    fabric.run(); // enters MemLink run loop
    return 0;
}